var Example = Example || {};

Example.car = function() {
    var Engine = Matter.Engine,
        Render = Matter.Render,
        Runner = Matter.Runner,
        Composites = Matter.Composites,
        MouseConstraint = Matter.MouseConstraint,
        Mouse = Matter.Mouse,
        World = Matter.World,
        Body = Matter.Body,
        Bodies = Matter.Bodies;

    // create engine
    var engine = Engine.create(),
        world = engine.world;

    // create renderer
    var render = Render.create({
        element: document.body,
        engine: engine,
        options: {
            width: 800,
            height: 600,
            showAngleIndicator: true,
            showCollisions: true
        }
    });

    Render.run(render);

    // create runner
    var runner = Runner.create();
    Runner.run(engine);

    // add bodies
    World.add(world, [
    // walls
        Bodies.rectangle(400, 0, 800, 50, { isStatic: true }), // 矩形的中心坐标（400, 0）。左上角为原点，横轴为x轴，向右为正；纵轴为y轴，向下为正。宽800，高50。
        Bodies.rectangle(400, 600, 800, 50, { isStatic: true }),
        Bodies.rectangle(800, 300, 50, 600, { isStatic: true }),
        Bodies.rectangle(0, 300, 50, 600, { isStatic: true })
    ]);

    // var scale = 0.9;
    var larRadius = 30;
    var largeCar = Bodies.circle(100, 575-larRadius, larRadius, {
        friction: 0,
        frictionStatic: 0,
        frictionAir: 0
    });
    World.add(world, largeCar);
    window.largeCar = largeCar;

    // scale = 0.8;
    // var smallCar = Composites.car(350, 563, 150 * scale, 30 * scale, 30 * scale);
    // console.log(smallCar);
    // World.add(world, smallCar);
    // window.smallCar = smallCar;
    var smaRadius = 20;
    var smallCar = Bodies.circle(200, 575-smaRadius, smaRadius, {
        friction: 0,
        frictionStatic: 0,
        frictionAir: 0,
        density:0.1,
    });
    World.add(world, smallCar);
    window.smallCar = smallCar;
    // Body.set(smallCar, { velocity: {x:20, y:0} });

    // World.add(world, [
    // Bodies.rectangle(200, 150, 400, 20, { isStatic: true, angle: Math.PI * 0.06 }),
    // Bodies.rectangle(500, 350, 650, 20, { isStatic: true, angle: -Math.PI * 0.06 }),
    // Bodies.rectangle(300, 560, 600, 20, { isStatic: true, angle: Math.PI * 0.04 })
    // ]);

    // add mouse control
    var mouse = Mouse.create(render.canvas),
        mouseConstraint = MouseConstraint.create(engine, {
            mouse: mouse,
            constraint: {
                stiffness: 0.2,
                render: {
                    visible: false
                }
            }
        });

    World.add(world, mouseConstraint);

    // keep the mouse in sync with rendering
    render.mouse = mouse;

    // fit the render viewport to the scene
    Render.lookAt(render, {
        min: { x: 0, y: 0 },
        max: { x: 800, y: 600 }
    });

    // context for MatterTools.Demo
    return {
        engine: engine,
        runner: runner,
        render: render,
        canvas: render.canvas,
        stop: function() {
            Matter.Render.stop(render);
            Matter.Runner.stop(runner);
        }
    };
};

if (typeof module !== "undefined") {
    module.exports = Example[Object.keys(Example)[0]];
}
